<?php

namespace App\Console\Commands;

use App\AdvertiserVideo;
use App\Service\DunVideoService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class ScheduleQueryVideo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'schedule:queryVideo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info("定时任务queryVideo开始", [
            'timestamp' => time()
        ]);
        $dunVideoService = DunVideoService::getInstance();
        $dunVideoService->setConfig(
            \config('app.dun_video_secret_id'),
            \config('app.dun_video_secret_key')
        );

        AdvertiserVideo::query()->where('status', 1)->chunkById(20, function ($dataList) use ($dunVideoService){
            $dataList = $dataList->toArray();
            $taskIds = array_column($dataList, 'task_id');
//            $taskIds = ['nzvii7wrt3ug81j00zyfkp0h0200aa51'];
            $params = array(
                "taskIds"=>json_encode($taskIds)
            );
            $ret = $dunVideoService->query($params);

            if (isset($ret['code']) && $ret['code'] == 200) {
                foreach ($ret['result'] as $item) {
                    if (isset($item['status']) && $item['status'] == 0) {
                        $dataId = $item['dataId'] ?? '';
                        if (!empty($dataId)){
                            $labelVideo = $labelVoice = [];
                            if (isset($item['evidences']['video']) && $item['evidences']['video']){
                                if ($item['evidences']['video']['level'] > 0){
                                    $labelVideo[] = [
                                        'tagGroup' => AdvertiserVideo::LABEL[$item['evidences']['video']['suggestReason']] ?? '',
                                        'tagName' => AdvertiserVideo::LABEL[$item['evidences']['video']['suggestReason']] ?? ''
                                    ];
                                    foreach ($item['evidences']['video']['evidences'] as $evidence){
                                            foreach ($evidence['labels'] as $l){
                                                foreach ($l['subLabels'] as $subLabel){
                                                    if (isset($subLabel['details']['imageTagInfos']) && $subLabel['details']['imageTagInfos']) {
                                                        foreach ($subLabel['details']['imageTagInfos'] as $info){
                                                            $labelVideo[] = [
                                                                'tagGroup' => $info['tagGroup'],
                                                                'tagName' => $info['tagName']
                                                            ];
                                                        }
                                                    }
                                                }
                                            }
                                    }
                                }
                            }

                            if (isset($item['evidences']['audio']) && $item['evidences']['audio']) {
                                if (isset($item['evidences']['audio']['label']) && $item['evidences']['audio']['label'] > 0) {
                                    $labelVoice[] = [
                                        'tagGroup' => AdvertiserVideo::LABEL[$item['evidences']['audio']['label']] ?? '',
                                        'tagName' => AdvertiserVideo::LABEL[$item['evidences']['audio']['label']] ?? ''
                                    ];
                                }
                            }

                            $model = AdvertiserVideo::query()
                                ->where('video_id', $dataId)
                                ->where('status', 1)
                                ->first();
                            if ($model){
                                $suggestReason = $item['result'] == 2 ? 4 : $item['result'];
                                $model->status = 2;
                                $model->suggestion = $suggestReason;
                                $model->censor_time = Carbon::now();
                                $model->label = json_encode([
                                    'video' => $labelVideo,
                                    'audio' => $labelVoice
                                ]);
                                $model->save();
                            }
                        }
                    }
                }
            }

            usleep(2000);
        });
        return 0;
    }
}
